﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Enterprises;

namespace MyMES.Writer.API.Application.CommendHandlers.Enterprises
{
    public class CreateEnterprisesCommendHandlers : IRequestHandler<CreateEnterprisesCommends, APIResult<Enterprise>>
    {
        private readonly IBaseRepository<Enterprise> enterpriseRepository;
        private readonly IMapper mapper;

        public CreateEnterprisesCommendHandlers(IBaseRepository<Enterprise> enterpriseRepository, IMapper mapper)
        {
            this.enterpriseRepository = enterpriseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加企业
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<Enterprise>> Handle(CreateEnterprisesCommends request, CancellationToken cancellationToken)
        {
            APIResult<Enterprise> result=new APIResult<Enterprise>();
            result.Code = APIEnum.添加成功;
            result.Msg = "添加成功";
     
            var sql = "INSERT INTO enterprise VALUES(@EnterpriseID,@EnterpriseNo, @EnterpriseName,@DepartmentID)";
            var enter = mapper.Map<Enterprise>(request);
            await enterpriseRepository.Insert(enter, sql);
            return result;
          
        }
    }
}
